package com.ashlikun.xrecycleview.simple

import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ashlikun.adapter.recyclerview.common.CommonAdapter
import com.ashlikun.adapter.recyclerview.section.SectionAdapter
import com.ashlikun.adapter.recyclerview.vlayout.MultipleAdapterHelp
import com.ashlikun.adapter.recyclerview.vlayout.mode.LayoutStyle
import com.ashlikun.vlayout.VirtualLayoutManager
import com.ashlikun.xrecycleview.divider.HorizontalDivider
import com.ashlikun.xrecycleview.listener.RecycleViewSwipeListener
import com.ashlikun.xrecycleview.simple.databinding.ActivityTest2Binding
import com.ashlikun.xrecycleview.simple.databinding.ItemHeaderBinding
import com.ashlikun.xrecycleview.simple.databinding.ItemView1Binding
import com.ashlikun.xrecycleview.simple.databinding.ItemViewBinding

class Test2Activity : AppCompatActivity(), RecycleViewSwipeListener {

    var data = ArrayList<NeibuData>()
    val binding by lazy {
        ActivityTest2Binding.inflate(layoutInflater)
    }
    val adapter by lazy {
        CommonAdapter(this,
            data,
            ItemView1Binding::class.java,
            onItemClick = {
                Toast.makeText(this, it.name, Toast.LENGTH_LONG).show()
            }) {
            binding<ItemView1Binding>().run {
                textView.setTextColor(0xfff13300.toInt())
                textView.text = it?.name
            }
        }
    }
    val statueHeaderBinding by lazy {
        ItemView1Binding.inflate(layoutInflater).apply {
            root.layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, 200)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        loadData()
        binding.apply {
            recyclerView.layoutManager = LinearLayoutManager(this@Test2Activity)
            recyclerView.itemAnimator = null
            //带头的可能最后一个有问题
            recyclerView.addItemDecoration(HorizontalDivider(this@Test2Activity, size = 20, color = 0xFFB9B9B9.toInt(), showLastDivider = false))
            recyclerView.adapter = adapter
            recyclerView.addHeaderView(statueHeaderBinding.root)
        }

    }

    override fun onLoadding() {
        binding.recyclerView.postDelayed({ loadData() }, 3000)
    }

    override fun onRefresh() {
        binding.recyclerView.postDelayed({ loadData() }, 3000)
    }

    fun loadData() {
        data.clear()
        for (j in 1..5) {
            data.add(NeibuData("我是type1,数据是$j"))
        }
        adapter.setDatas(data, true)
    }
}